## prelims
rm(list = setdiff(c(ls(),lsf.str()),c("scrdir","ab")))
source("regression_functions.R")
tab.path <- "results/"

#####
#####a

ab$higheduc <- as.numeric(ab$educ > 2 )

yvars <- c( "identity_more_ethnic_sc")

croplab <- c(  "Cash crops USD pkm2 " ,  "Cash crops USD pkm2 x British Colony")

geovar <- c("crop_poly_sc", "pubspc.23_poly_sc")
langvar <- c("crop_lang_sc", "pubspc.23_lang_sc" )
labs <- c("Cash crops USD pkm2 ", "Pubs pth pop (1923)")

controls.ind <- c("sex", "age", "age2", "higheduc", "formal_empl",   "ea_age" , "ea_school", "ea_clinic"  )
controls.geo <- c("urban","ruggedness_nunn_puga" ,  "elevation_mean"  ,  "malaria_suit_max",
"TSI_CRU_mean_1901_1920" , "temperature_fao" ,  "coast_log"  ,
"longitude", "agric_suit", "latitude","lpopc_hyde_meanprecol_poly")

controls.hist <- c("explorers_log", "cities_log", "capital_log" , "dist.prot_log" , "dist.print_log"  )


i <- 1
base.x1 <- c(controls.ind, "lpopc_hyde_meanprecol_lang")
base.x2 <- c(controls.ind, controls.geo, controls.hist)
# controls.geo,controls.hist)



m1 <- felm(as.formula(RegFor( y = yvars[i] , x = c( geovar, base.x2) ,
          FE = "country_survey_round" , IV="0", clust = "loc.id" )),
          data=ab  )
m2 <- felm(as.formula(RegFor( y = yvars[i] , x = c( geovar, base.x2) ,
          FE = " country_survey_round" , IV="0", clust = "loc.id" )),
          data=ab , subset = in.soas.23_poly  == 1   )

m3 <- felm(as.formula(RegFor( y = yvars[i] , x = c( langvar, base.x1) ,
                      FE = "loc.id" , IV="0", clust = "loc.id" )),
                      data=ab  )

m4<- felm(as.formula(RegFor( y = yvars[i] , x = c( langvar, base.x1) ,
                      FE = "loc.id" , IV="0", clust = "loc.id" )),
                      data=ab , subset =  in.soas.23_poly  == 1   )


## Output tables
    getwd()
    keepvars <- c(geovar, c("urban" , "sex", "formal_empl", "higheduc"), langvar)
    keeplabs <- c( labs, "Urban", "Female", "Formal Employment", "Education" , labs)

m.all <- round(mean(ab[,yvars[i]], na.rm = TRUE), 4)
sd.all <- round(sd(ab[,yvars[i]], na.rm = TRUE), 4)

yss <- ab %>%
        filter(in.soas.23_poly  == 1 ) %>%
        summarize_at( yvars[i] , funs(mean( ., na.rm =T)
        , sd(. , na.rm =T))) %>%
        mutate_all(~round(.,2))
yss2 <- ab %>%
        filter(in.soas.23_poly  == 1 & missions_yn_max_lang == 1 ) %>%
        summarize_at( yvars[i] , funs(mean( ., na.rm =T)
        , sd(. , na.rm =T))) %>%
        mutate_all(~round(.,2))

    ####OUT
    add.lines <- list( AddLines( 4, "Individual controls" , rep(LatMc("Yes",1), 4) ) ,
      AddLines( 4, "Historical and Geo controls" , c(rep(LatMc("No",1), 2) , rep(LatMc("Yes",1), 2 ) ) ) ,
      AddLines( 4, "Fixed Effect" , c(rep(LatMc("Country-Round",1),2) , rep(LatMc("Town",1),2) )) ,
      AddLines( 4, "Sample" , rep( c(LatMc("All",1), LatMc("In Biblio",1)), 2)  ) ,
      AddLines( 4, "Mean dep. var." , rep(c(LatMc(m.all,1), LatMc(yss[i],1)), 2) )
     )

    m.list <- list(m1,m2, m3, m4 )

    tab.name <- paste0("ab_tableA3", ".tex")
    fileConn<-file(paste0(tab.path,tab.name))
    print(fileConn)
    writeLines(stargazer(m.list,
                         float = FALSE ,
                         keep=keepvars,
                         order = keepvars ,
                         covariate.labels= keeplabs ,
                         star.cutoffs = c(0.109, 0.0509, 0.019) ,
                         digits.extra = 0,
                         multicolumn=F,# se = se,
                         column.sep.width="10pt",
                         dep.var.caption = "",
                         dep.var.labels.include = F,
                         column.labels = c("Geograhic-level", "Ethnic-level"),
                         column.separate = c(2,2),
                         font.size = "scriptsize",
                         omit.table.layout = "n",
                         align =T,
                         add.lines = add.lines,digits = 3, intercept.top = T,intercept.bottom = F,
                         keep.stat = c("rsq" , "n") ,
                         omit.stat = c("res.dev","ser") ),
                         fileConn)
  close(fileConn)
